#!/usr/bin/env python

import os, sys
import mad
import glob, math

def executecmd(cmdstring):
	print cmdstring
	os.system(cmdstring)
	
 
if (len(sys.argv) > 1):
	path = sys.argv[1]
else:
	path = '.'

for infile in glob.glob( os.path.join(path, '*done.mpg') ):	

	# set filename
	workingfileprefix = infile.rstrip('.mpg').lstrip('./')
	print(workingfileprefix)

	#determine length
	mp3name = workingfileprefix + ".mp3";
	mf = mad.MadFile(mp3name)
	track_length_in_milliseconds = mf.total_time()
	
	number_of_10min_chunks = math.ceil(track_length_in_milliseconds / 600000.0)
	print ("track_length_in_milliseconds=%u, number_of_10min_chunks=%s" % (track_length_in_milliseconds, number_of_10min_chunks))

	# split if longer than 10 mins
	if number_of_10min_chunks > 1:
		executecmd(('/home/emlyn/dev/vidworking/splitforyoutube.py %s.mpg %u %s-') % (workingfileprefix, number_of_10min_chunks, workingfileprefix))
		
print "Done."


